package cn.hs.lyxf.basic;

import com.itextpdf.text.DocumentException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Package: cn.hs.lyxf.basic
 * @ClassName: SSS
 * @Date:2022/12/8 18:31
 * @Description:
 */
public class JavaToPdfHtmlFreeMarkerTest {

    // 模板名称
    String HTML = "contract.html";
    // 生成的pdf存放路径
    String DEST = "D:\\app\\user_contract\\";
    // 生成的pdf名称
    String fileName = "合同D20210511SDNQB1.pdf";

    public static void main(String[] args) throws IOException, DocumentException, InterruptedException, com.lowagie.text.DocumentException {
        Map<String, Object> data = new HashMap();
        data.put("开始年月日","2022年12月8日");
        data.put("到期年月日","2027年12月8日");
        data.put("合同编号", "D20210511SDNQB");
        data.put("甲方姓名", "小哈");
        data.put("甲方负责人", "小哈");
        data.put("签字日期", "2021.05.13");
        String content = JavaToPdfHtmlFreeMarker.freeMarkerRender(data, "h.html");
        JavaToPdfHtmlFreeMarker.createPdf(content, "D:\\lyxf\\" + "合同D20210511SDNQB1.pdf");



//        // base64格式的图片
//        String signData = '';
//        // 生成电子签字
//        String oldPdfUrl= "D:\\lyxf\\"+"合同D20210511SDNQB1.pdf";
//        String newPdfUrl= "D:\\lyxf\\"+"合同D20210511SDNQB.pdf";
//        //查找签名位置
//        float[] position= PdfKeywordFinder.getAddImagePositionXY(oldPdfUrl,"(乙方)签章：");
//        //Read file using PdfReader
//        PdfReader pdfReader = new PdfReader(oldPdfUrl);
//        System.out.println("x:"+position[1]+" y:"+position[2]);
//        PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(newPdfUrl));
//        String imgUrl = Base64ImgConvert.convertBase64ToPng(signData, contractNo);
//        Image image = Image.getInstance(imgUrl);
//        //Fixed Positioning
//        image.scaleAbsolute(100, 50);
//        //Scale to new height and new width of image
//        image.setAbsolutePosition(position[1], position[2]);
//        System.out.println("pages:"+pdfReader.getNumberOfPages());
//        PdfContentByte content2 = pdfStamper.getUnderContent((int) position[0]);
//        content2.addImage(image);
//        pdfStamper.close();
    }
}
